package com.mengwen.backstage.controller;

import com.mengwen.backstage.pojo.*;
import com.mengwen.backstage.service.GetNotificationService;
import com.mengwen.backstage.service.NotificationService;
import com.mengwen.backstage.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * @author LiangYonghui
 * @date 2020/3/30 21:31
 * @description 有关合伙人发送通知的
 */
@Controller
public class NoticeController {
    private static final Log LOG = LogFactory.getLog(NoticeController.class);

    @Autowired
    NotificationService notificationService;
    @Autowired
    GetNotificationService getNotificationService;


    /**
     * 跳转到发送消息的页面
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/to_send_page")
    public String toSendPage(HttpServletRequest request, Model model){
        String partnerToken = request.getParameter("partner_token");
        Claims claims = JwtUtil.checkJWT(partnerToken);
        String partnerNickname = (String) claims.get("partnerNickname");
        String role = (String) claims.get("role");
        if ("partner".equals(role)) {
            model.addAttribute("partnerToken",partnerToken);
            model.addAttribute("openid",claims.get("partnerOpenid"));
            model.addAttribute("unionid",claims.get("partnerUnionid"));
            return "send";
        }else {
            return "login_door";
        }


    }


    /**
     * 发送通知
     * 将数据存储到通知表中
     * @param sendNotificationDto
     * @return
     */
    @ResponseBody
    @PostMapping("/send")
    public Result noticeToSmallApp(@RequestBody SendNotificationDto sendNotificationDto, HttpServletRequest request, Model model) {
        Claims claims = JwtUtil.checkJWT(sendNotificationDto.getToken());
        String role = (String) claims.get("role");
        String unionid = (String) claims.get("partnerUnionid");
        String openid = (String) claims.get("partnerOpenid");
        String partnerNickname = (String) claims.get("partnerNickname");
        System.out.println(unionid + openid + partnerNickname);
        if ("partner".equals(role) && (null != partnerNickname) && (null != unionid) && (null != openid)) {
            // 执行注入表的操作
            Notification notification = new Notification();
            notification.setContent(sendNotificationDto.getContent());
            notification.setNickname(partnerNickname);
            notification.setNoticeDate(new Date());
            notification.setUnionid(unionid);
            notification.setOpenid(openid);
            notification.setNotificationId( UUID.randomUUID().toString());
            if (sendNotificationDto.isToPC()){
                notification.setTopc(1);
            }
            if (sendNotificationDto.isToSmallApp()) {
                notification.setTosmapp(1);
            }
            int add = notificationService.addNotification(notification);
            if (add < 0) {
                LOG.info(partnerNickname + "插入通知失败，unionid：" + unionid +  ",openid:" + openid);
                return new Result(false, StatusCode.ERROR, "发送失败，请联系管理员");
            }
            LOG.info(partnerNickname + ",发送通知成功，unionid：" + unionid + ",openid:" + openid);
            return new Result(true, StatusCode.OK, "发送成功");
        }else {
            return new Result(false, StatusCode.ERROR, "发送失败，请正确登录");
        }
    }



    /**
     * 获取通知
     * @return
     */
    @ResponseBody
    @RequestMapping("get_notification/{id}")
    public GetNotificationDto smallAppGetNitification(@PathVariable("id") String unionid){
        List<GetNotification> notifications = getNotificationService.findNotReadNotificationByUnionid(unionid);
        GetNotificationDto getNotificationDto = new GetNotificationDto();
        if (notifications == null) { // 无新通知
            LOG.info("通知内容条数为空。");
            getNotificationDto.setNotificationSize(0);
            getNotificationDto.setNotifications(null);
        }else {
            getNotificationDto.setNotificationSize(notifications.size());
            getNotificationDto.setNotifications(notifications);
        }


        return getNotificationDto;
    }


}
